import { Component, EventEmitter, Input, OnInit, Output, TemplateRef, ViewChild } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { SFSchema } from '@delon/form';
import { MenuService } from '../../menu-manage/menu.service';
import { UtilService } from '@core/services/util.service';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: `app-role-modal`,
  templateUrl: './role.component.html',
  styleUrls: ['./index.less'],
})
export class RoleComponent implements OnInit {
  @Input() type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = JSON.parse(JSON.stringify(value));
    this.param.permition = value.permissions.map(item => item.id);
    console.log(this.param.permition)
  };
  param: any;
  showForm = false;
  schema: SFSchema = {
    properties: {
      roleSortno: {
        type: 'string',
        title: '角色编号',
      },
      roleName: {
        type: 'string',
        title: '角色名称',
      },
      permition: {
        type: 'string',
        title: '功能权限',
        enum: [],
        ui: {
          widget: 'sf-tree-select',
          checkable: true,
          multiple: true,
          nzCheckStrictly: true,
          dropdownStyle: {
            'max-height': '300px',
          }
        },
      },
      status: {
        type: 'string',
        title: '数据状态',
        enum: [
          {label: '启用', value: true},
          {label: '停用', value: false},
        ],
        ui: {
          widget: 'select',
        },
      },
      roleRemark: {
        type: 'string',
        title: '备注',
      },
    },
    required: ['roleSortno', 'roleName', 'status'],
  };

  constructor(private msg: NzMessageService,
              private modalRef: NzModalRef,
              private menu: MenuService,
              private util: UtilService,
              private api: ApiService,
              private http: HttpClient
  ) {
    this.menu.getMenuList({}).subscribe( res => {
      if (res.status === '200' && res.data && res.data.length > 0) {
        const arr = res.data.map(item => {
          return {key: item.id, pId: item.menuParentId, title: item.menuName, id: item.id};
        })
        this.schema.properties.permition.enum = this.util.getTreeNodeOption(arr);
        this.showForm = true;
      }
    })
  }

  ngOnInit() {

  }

  // 确定编辑功能
  formSubmit(data) {
    const body = JSON.parse(JSON.stringify(data));
    delete body.permition;
    delete body._values;
    body.permissions = data.permition.map(item => {
      return {id: item};
    })
    body.roleType = '1';
    const url = this.type === 0 ? this.api.role.add.url : this.api.role.update.url;
    const method = this.type === 0 ? this.api.role.add.method : this.api.role.update.method
    this.http.request<any>(method, url,  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })

  }
}
